#include "includes.h"

uint64_t macs[] =
{
    0xD4C6394E713B ,
    0xC75DAD9DA384 ,
    0xC7D03512826A ,
    0x91EC872681C0 ,
    0x0AEBB5FB261E ,
    0xEAE7C8CF1D8F ,
    0x027EF38F418D ,
    0xCE167C7CA50B ,
    0xA3B9464FFFD0 ,
    0x102B67D08B3B ,
    0xA58E9363E78E ,
    0x78109B6F2A6E ,
    0x6CC833BFE6F2 ,
    0x56EF8F3C2F9B ,
    0x2AE73ED68880 ,
    0x446F560FB2CE ,
    0x69BB449EE678 ,
    0x905430E9190C ,
    0xC74F3E1F3D09 ,
    0x63E48D695E47 ,
    0xDDC111B8F52F ,
    0x5CDCE0B32462 ,
    0x4C6FCEDA38F6 ,
    0xBB399A84F887 ,
    0x2F6EFA743E2F ,
    0xFB17F74073E2 ,
    0x6FDF6742ABCF ,
    0x83AAAC38C99E ,
    0x38586D3BC468 ,
    0x11FD897D547B ,
    0x59FF3A615260 ,
    0xDF6CCA95A2D7 ,
    0x1E6F9F22E19C ,
    0xABC6617BC200 ,
    0x9841659E3C31 ,
    0xB710E46871CC ,
    0x438BC19E9846 ,
    0xDC6137C886F2 ,
    0xE0FF2F5C1F97 ,
    0x47DC21C594F9 ,
    0x3EFB18D9EBB3 ,
    0x06C27D1E56A0 ,
    0xD439486DD686 ,
    0x598AA110B025 ,
    0x277D240D6776 ,
    0xC11ACE9DE03F ,
    0xBBD703419059 ,
    0xF98AC69F85C4 ,
    0x67E812C28741 ,
    0x69FD86DB7B83 ,
    0xAD2CF4E67619 ,
    0x6472E755CD22 ,
    0xD850BA2AC230 ,
    0x5C6A445B407B ,
    0x35B1A1F82F85 ,
    0xFBAEEB6BBC58 ,
    0x095581DFF43F ,
    0xEB92AC2F1510 ,
    0xD2862D6A77C8 ,
    0x0D97DD9EEA2C ,
    0x107DF9F596C3 ,
    0xD0CEE16F9775 ,
    0x015761AAA621 ,
    0x7B1E546B88DD ,
    0xD6C84089608E ,
    0xC703DC4AF506 ,
    0x5D11F0228046 ,
    0x8614F37A97A5 ,
    0xDF2FA5157286 ,
    0x3BFE44F4025C ,
    0xF72C3216EB90 ,
    0x4992ACFCC6C3 ,
    0x58734274F350 ,
    0xADBC3CC349BC ,
    0x6274C08F1457 ,
    0x9B9A0630C0F9 ,
    0x751A1B21677F ,
    0x299ADA547E51 ,
    0x48223F1E37F9 ,
    0xA5256CF4FD3C
};

uint8_t vdata_find_mac(uint64_t *brecv_ytes)
{
    uint16_t i = 0, count = 0, dev_num = 0, mac_index = 0;
    uint64_t mask = 0, mac_half = 0, recv_mac = 0, recv_half = 0;
    for(i = 0; i < 80; i++){
        count = count + 1;
        mask = ((1 << (4 * (brecv_ytes[0] + 1))) - 1);
        mac_half = macs[i] & mask;
        recv_mac = (brecv_ytes[1] << 0) + (brecv_ytes[2] << 8) + (brecv_ytes[3] << 16) + (brecv_ytes[4] << 24) + (brecv_ytes[5] << 32) + (brecv_ytes[6] << 40);
        recv_half = recv_mac & mask;
        if(mac_half == recv_half){
            mac_index = count;
            dev_num = dev_num + 1;
        }
        if(count == 80){
            if(dev_num == 0){
                printf("none mac ...");
                return 0;
            }else if(dev_num == 1){
                printf("only one mac ...");
                return 1;
            }else {
                printf("have multi mac ...");
                return 2;
            }
        }
    }
}

void request_mac_logic(uint64_t init_addr, uint8_t index)
{
    uint8_t i = 0, j = 0, res = 0;
    uint64_t find_mac = 0;
    uint8_t command[10];

    for(i = index; i < 12; i++){
        for(j = 0; j < 16; j++){
            find_mac = init_addr + (j << (index * 4));
            printf("[%d] -> %x\r\n", i, find_mac);
            command[0] = i;
            command[1] = ((find_mac >> 8) & 0xFF);
            command[2] = ((find_mac >> 16) & 0xFF);
            command[3] = ((find_mac >> 24) & 0xFF);
            command[4] = ((find_mac >> 32) & 0xFF);
            command[5] = ((find_mac >> 40) & 0xFF);
            command[6] = ((find_mac >> 48) & 0xFF);
            res = vdata_find_mac(command);
            if(res == 2){
                request_mac_logic(find_mac, i + 1);
            }else{
                break;
            }
        }
    }
}

int main(void)
{
    request_mac_logic(0, 0);
    return 0;
}
